///\todo upgrade

//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#ifndef _CFONT_H_
#define _CFONT_H_

#include <buola/image.h>
#include <buola/image/cfontinfo.h>
#include <buola/image/cfontmetrics.h>
#include <buola/image/glyph.h>
#include <buola/image/ctextlayout.h>
#include <buola/image/clayoutengine.h>
#include <unordered_map>

namespace buola { namespace img {

class CFontFace;
class CScaledFont;

///\addtogroup image
///@{

////////////////////////////////////////////////////////////////////////////

/// font

///
///
///
////////////////////////////////////////////////////////////////////////////

class CFont : public UShared
{
    struct SFont;

protected:
    CFont(const CFontInfo&);
    virtual ~CFont();

public:
    const CFontInfo &Info()             {   return mInfo;       }
    const CFontMetrics &Metrics()       {   return mMetrics;    }

    double GetWidth(const std::wstring &pStr);
    double GetHeight()                  {   return mMetrics.mHeight;    }

    static PFont Get(const CFontInfo&);
    static PFont GetStock(const std::wstring&);

    static void GetFamilyList(std::vector<std::string> &pList);
    static void GetStyleList(const std::string &pFamily,std::vector<std::string> &pList);

    CLayoutEngine *GetEngine(const CTextLayout::SAnalysis &pAnalysis);

public:
    int GetNumGlyphs(TFontIndex pF);
    bool GetFontTable(TFontIndex pF,uint32_t pTag,void *pDst,unsigned int &pLen);
    TFontIndex GetSubFont(wchar_t pC);
    bool HasChar(wchar_t pC,TFontIndex pI);
    TGlyphIndex GetGlyph(wchar_t pC,TFontIndex pFont);
    void FillGlyphInfo(CGlyphInfo &pG);
    int GetAdvance(TFontIndex pFont,TGlyphIndex pGlyph);
    int GetUnitsPerEM(TFontIndex pFont);

public:
    CFontFace *GetFontFace(int i);
    CScaledFont *GetScaledFont(int i);

private:
    void CalcMetrics();

private:
    CFontInfo mInfo;
    CFontMetrics mMetrics;
    std::vector<SFont> mFonts;

    std::unordered_map<std::pair<unsigned int,unsigned int>,CLayoutEngine*,APairHash> mEngines;

    static std::unordered_map<std::wstring,PFont> mStockFonts;
    static std::unordered_map<CFontInfo,PFont> mAllFonts;
    static recursive_mutex mStaticMutex;
};

///@}

/*namespace img*/ } /*namespace buola*/ }

#endif
